/***************
 * Home Imports Panel
 */
AIR2.Home.Imports = function() {
    // determine if the upload button should be visible
    var mayUpload = false;
    if (AIR2.USERINFO.type == "S") {
        mayUpload = true;
    }
    else {
        // can upload with WRITER role in any organization
        Ext.iterate(AIR2.USERAUTHZ, function(uuid, role) {
            if (AIR2.AUTHZ.WRITER & role) mayUpload = true;
        });
    }

    var p = new AIR2.UI.Panel({
        colspan: 1,
        title: 'Imports',
        cls: 'air2-home-import',
        iconCls: 'air2-icon-import',
        tools: ['->',{
            xtype: 'air2button',
            air2type: 'CLEAR',
            iconCls: 'air2-icon-fileup',
            tooltip: 'Upload CSV',
            hidden: !mayUpload,
            handler: function() {
                var w = AIR2.Upload.Modal({originEl: this.el});
                w.on('close', function() {
                    p.reload();
                });
            }
        }],
        showTotal: true,
        url: AIR2.HOMEURL+'/import',
        storeData: AIR2.Home.IMPDATA,
        itemSelector: '.air2-home-import-row',
        tpl: new Ext.XTemplate(
            '<table><tpl for=".">' +
              '<tr class="air2-home-import-row">' +
                '<td class="icon-col">{[AIR2.Format.tankType(values)]}</td>' +
                '<td class="name">{[AIR2.Format.tankName(values,true)]}</td>' +
                '<td class="date" align="right">{[AIR2.Format.date(values.tank_upd_dtim)]}</td>' +
                '<td class="icon-col">{[AIR2.Format.tankStatus(values)]}</td>' +
              '</tr>' +
            '</tpl></table>',
            {compiled:true, disableFormats:true}
        ),
        editModal: {
            allowAdd: false,
            width: 650,
            items: {
                xtype: 'air2liveeditor',
                url: AIR2.HOMEURL+'/import',
                sortField: 'tank_upd_dtim',
                sortDir: 'DESC',
                pageSize: 50,
                hideButtons: true,
                tpl: new Ext.XTemplate(
                    '<table style="width:100%">' +
                      '<tr class="air2-home-import-row">' +
                        '<td class="icon-col">{[AIR2.Format.tankType(values)]}</td>' +
                        '<td class="name">{[AIR2.Format.tankName(values,true)]}</td>' +
                        '<td align="right">{[AIR2.Format.userName(values.User, true)]}</td>' +
                        '<td class="date date-long" align="right">{[AIR2.Format.dateLong(values.tank_upd_dtim)]}</td>' +
                        '<td class="icon-col left-pad">{[AIR2.Format.tankStatus(values)]}</td>' +
                        '<td class="status">{[AIR2.Format.tankStatus(values, true)]}</td>' +
                      '</tr>' +
                    '</table>',
                    { compiled: true, disableFormats: true }
                )
            }
        }
    });

    return p;
}